#include <stdio.h>

#define I 0
#define V 1
#define X 2
#define L 3
#define C 4
#define D 5
#define M 6

int vt[]={  [I]=1,
            [V]=5,
            [X]=10,
            [L]=50,
            [C]=100,
            [D]=500,
            [M]=1000
        };


int romanToInt(char* s) {
    char lastCh;
    int of=0,num=0;
    while( s[of] != '\0' ){
        if( s[of] == 'I' )
            num+=vt[I];
        else if( s[of] == 'V' ){
            num+=vt[V];
            if( of>0 && lastCh == 'I' )
                num-=vt[I]*2;
        }else if( s[of] == 'X' ){
            num+=vt[X];
            if( of>0 && lastCh == 'I' )
                num-=vt[I]*2;
        }else if( s[of] == 'L' ){
            num+=vt[L];
            if( of>0 && lastCh == 'X' )
                num-=vt[X]*2;
        }else if( s[of] == 'C' ){
            num+=vt[C];
            if( of>0 && lastCh == 'X' )
                num-=vt[X]*2;
        }else if( s[of] == 'D' ){
            num+=vt[D];
            if( of>0 && lastCh == 'C' )
                num-=vt[C]*2;
        }else if( s[of] == 'M' ){
            num+=vt[M];
            if( of>0 && lastCh == 'C' )
                num-=vt[C]*2;
        }
        lastCh=s[of];
        of++;
    }
    return num;
}

int main(){
    printf("%d\n",romanToInt("MCMXCIV"));
    return 0;
}
